package com.seeyon.apps.kk.manager;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.seeyon.apps.kk.constants.Constants;
import com.seeyon.cap4.form.api.FormApi4Cap4;
import com.seeyon.cap4.form.bean.FormBean;
import com.seeyon.cap4.form.bean.FormDataMasterBean;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.exceptions.BusinessException;
import com.seeyon.ctp.organization.bo.V3xOrgMember;
import com.seeyon.ctp.organization.manager.OrgManager;
import com.seeyon.ctp.util.FlipInfo;

public class CollaborationLogManager {

	private static final Log log = LogFactory.getLog(CollaborationLogManager.class);
	FormApi4Cap4 formApi4Cap4 = (FormApi4Cap4) AppContext.getBean("formApi4Cap4");
	OrgManager orgManager = (OrgManager) AppContext.getBean("orgManager");
	
	public JSONObject getAllCollaborationLog() {
		JSONObject resultObj=new JSONObject();
		JSONArray jsonArray = new JSONArray();
		FlipInfo flipInfo = new FlipInfo(1, 10000);
	    
		try {
			FormBean bean = formApi4Cap4.getFormByFormCode(Constants.nodeFormCode);
			List<FormDataMasterBean> masterList = formApi4Cap4.findMasterDataList(flipInfo, bean.getId(), null, null );
			log.info("##强生客开--------------masterList size: " + masterList.size());
			
			if(CollectionUtils.isNotEmpty(masterList)) {
				for(FormDataMasterBean masterBean : masterList) {
					JSONObject obj = new JSONObject();
					obj.put("表单名", masterBean.getFieldValue("field0003"));
					obj.put("表单标题", masterBean.getFieldValue("field0004"));
					obj.put("流程节点名", masterBean.getFieldValue("field0005"));
					V3xOrgMember  member = orgManager.getMemberById(Long.valueOf(masterBean.getFieldValue("field0006").toString()));
					obj.put("操作人", member.getName());
					obj.put("操作人wwid", masterBean.getFieldValue("field0022"));
					obj.put("操作时间", masterBean.getFieldValue("field0007").toString());
					obj.put("动作内容", masterBean.getFieldValue("field0008"));
					obj.put("接收时间", masterBean.getFieldValue("field0009").toString());
					obj.put("审批意见", masterBean.getFieldValue("field0011"));
					obj.put("审批状态", masterBean.getFieldValue("field0012"));
					obj.put("有效处理时长", masterBean.getFieldValue("field0014"));
					obj.put("流程ID", masterBean.getFieldValue("field0016"));
					obj.put("操作说明", masterBean.getFieldValue("field0018"));
					obj.put("首次查看时间", masterBean.getFieldValue("field0020") == null ? null : masterBean.getFieldValue("field0020").toString());
					jsonArray.add(obj);
				}
			}
			
			resultObj.put("result",jsonArray);
			
		} catch (BusinessException e) {
			e.printStackTrace();
			log.error("##强生客开异常--------------获取节点日志底表异常");
			return resultObj;
		}catch (SQLException e) {
			e.printStackTrace();
			log.error("##强生客开异常--------------查找节点日志数据异常");
			return resultObj;
		}
		
		return resultObj;
	}
	
}
